package response

import (
	"encoding/json"
	"github.com/spf13/cast"
	"gorm.io/datatypes"
	"strings"
)

type YtJqrRes struct {
	StatusCode int       `json:"statusCode"`
	Message    string    `json:"message"`
	Info       []JqrInfo `json:"info"`
}

type JqrInfo struct {
	Id          string `json:"id"`
	Name        string `json:"name"`
	TaskCoding  string `json:"task_coding"`
	BatchidStr  string `json:"batchid_str"`
	Status      string `json:"status"`
	BatchidName string `json:"batchid_name"`
}

type YtPhoneRes struct {
	StatusCode int    `json:"statusCode"`
	Message    string `json:"message"`
	Info       struct {
		Page       int       `json:"page"`
		PageSize   int       `json:"page_size"`
		TotalPage  int       `json:"total_page"`
		TotalCount string    `json:"total_count"`
		Info       []YtPhone `json:"info"`
	} `json:"info"`
}

type YtPhone struct {
	Id               string `json:"id"`
	CustomerId       string `json:"customer_id"`
	CallResultNumber string `json:"call_result_number"`
	CallDate         string `json:"call_date"`
	Dst              string `json:"dst"`
	UniqueId         string `json:"unique_id"`
	Path             string `json:"path"`
	CompanyName      string `json:"company_name"`
	Name             string `json:"name"`
	Scores           string `json:"scores"`
	Address          string `json:"address"`
	Billsec          string `json:"billsec"`
	BatchId          string `json:"batch_id"`
	RecordDetail     []struct {
		Msg        string `json:"msg"`
		User       string `json:"user"`
		Uniqueid   string `json:"uniqueid"`
		CreateTime string `json:"create_time"`
		Path       string `json:"path"`
	} `json:"record_detail"`
}

func (y *YtPhone) ToTag() datatypes.JSON {
	tagMap := make(map[string]any)
	tagMap["companyName"] = y.CompanyName
	tagMap["address"] = y.Address

	if cast.ToInt(y.Scores) > 90 {
		tagMap["level"] = 4
	} else if cast.ToInt(y.Scores) > 80 {
		tagMap["level"] = 3
	} else if cast.ToInt(y.Scores) > 70 {
		tagMap["level"] = 2
	} else if cast.ToInt(y.Scores) >= 60 {
		tagMap["level"] = 1
	}

	str, _ := json.Marshal(tagMap)
	return str
}

func (y *YtPhone) ToRemark() string {
	msgList := make([]string, 0, len(y.RecordDetail))
	for _, recordDetail := range y.RecordDetail {
		msgList = append(msgList, recordDetail.Msg)
	}
	if len(msgList) > 0 {
		return strings.Join(msgList, "\n")
	} else {
		return ""
	}
}
